using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class GotchaRewardList : MonoBehaviour
{
	public GotchaRewardUI rewardButton;

	public UIPanel scrollViewPanel;

	public UIScrollView scrollView;

	public Transform content;

	private UIWrapContent wrapContent;

	private List<GotchaRewardUI> rewardButtonList = new List<GotchaRewardUI>();

	public int iterationsCount = 50;

	public int rewardUIwidth = 200;

	public GameObject flashbulbsHolder;

	public UISprite[] flashbulbsRight;

	public UISprite[] flashbulbsLeft;

	private float anumarionTime;

	private Coroutine FlashbulbsRightAnimationCoroutine;

	private Coroutine FlashbulbsLeftAnimationCoroutine;

	private Coroutine FlashbulbsBlinkAnimationCoroutine;

	private Vector3 startContentPosition;

	private bool settingPreviewScroll;

	private float lerpMiltiplayer;

	private Vector3 newScrollPosition = Vector3.zero;

	private void StartFlashbulbsSpinAnimations()
	{
		StopFlashbulbsAnimations();
		flashbulbsHolder.SetActive(true);
		FlashbulbsRightAnimationCoroutine = CoroutineRunner.Instance.StartCoroutine(FlashbulbsRightAnimation());
		FlashbulbsLeftAnimationCoroutine = CoroutineRunner.Instance.StartCoroutine(FlashbulbsLeftAnimation());
	}

	private void StartFlashbulbsBlinkAnimations()
	{
		StopFlashbulbsAnimations();
		flashbulbsHolder.SetActive(true);
		FlashbulbsBlinkAnimationCoroutine = CoroutineRunner.Instance.StartCoroutine(FlashbulbsBlinkAnimation());
	}

	private void StopFlashbulbsAnimations()
	{
		if (FlashbulbsRightAnimationCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(FlashbulbsRightAnimationCoroutine);
		}
		if (FlashbulbsLeftAnimationCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(FlashbulbsLeftAnimationCoroutine);
		}
		if (FlashbulbsBlinkAnimationCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(FlashbulbsBlinkAnimationCoroutine);
		}
		UISprite[] array = flashbulbsRight;
		foreach (UISprite uISprite in array)
		{
			HOTween.Kill(uISprite);
			uISprite.alpha = 0f;
		}
		UISprite[] array2 = flashbulbsLeft;
		foreach (UISprite uISprite2 in array2)
		{
			HOTween.Kill(uISprite2);
			uISprite2.alpha = 0f;
		}
		flashbulbsHolder.SetActive(false);
	}

	private IEnumerator FlashbulbsRightAnimation()
	{
		anumarionTime = 0.02f;
		for (int count = 0; count < 9; count++)
		{
			for (int i = flashbulbsRight.Length - 1; i >= 0; i--)
			{
				HOTween.To(flashbulbsRight[i], anumarionTime, new TweenParms().Prop("alpha", 1f).Ease(EaseType.EaseInOutBack).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					BCWAudioResource.Instance.PlaySound("SpinBlinkClip");
					HOTween.To(flashbulbsRight[i], anumarionTime * 2f, new TweenParms().Prop("alpha", 0f).Ease(EaseType.EaseInExpo));
				}));
				yield return new WaitForSeconds(anumarionTime);
			}
		}
	}

	private IEnumerator FlashbulbsLeftAnimation()
	{
		for (int count = 0; count < 9; count++)
		{
			for (int i = 0; i < flashbulbsLeft.Length; i++)
			{
				HOTween.To(flashbulbsLeft[i], anumarionTime, new TweenParms().Prop("alpha", 1f).Ease(EaseType.EaseInOutBack).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					HOTween.To(flashbulbsLeft[i], anumarionTime * 2f, new TweenParms().Prop("alpha", 0f).Ease(EaseType.EaseInExpo));
				}));
				yield return new WaitForSeconds(anumarionTime);
			}
		}
	}

	private IEnumerator FlashbulbsBlinkAnimation()
	{
		int count = 0;
		while (count < 3)
		{
			UISprite[] array = flashbulbsRight;
			foreach (UISprite light4 in array)
			{
				HOTween.To(light4, 0.1f, new TweenParms().Prop("alpha", 1f).Ease(EaseType.EaseInOutBack));
			}
			UISprite[] array2 = flashbulbsLeft;
			foreach (UISprite light3 in array2)
			{
				HOTween.To(light3, 0.1f, new TweenParms().Prop("alpha", 1f).Ease(EaseType.EaseInOutBack));
			}
			BCWAudioResource.Instance.PlaySound("SpinFinalClip");
			yield return new WaitForSeconds(0.15f);
			UISprite[] array3 = flashbulbsRight;
			foreach (UISprite light2 in array3)
			{
				HOTween.To(light2, 0.2f, new TweenParms().Prop("alpha", 0f).Ease(EaseType.EaseInExpo));
			}
			UISprite[] array4 = flashbulbsLeft;
			foreach (UISprite light in array4)
			{
				HOTween.To(light, 0.2f, new TweenParms().Prop("alpha", 0f).Ease(EaseType.EaseInExpo));
			}
			count++;
			yield return new WaitForSeconds(0.2f);
		}
	}

	private void Awake()
	{
		rewardButton.gameObject.SetActive(false);
		startContentPosition = content.localPosition;
	}

	public void HideScroll()
	{
		StopFlashbulbsAnimations();
		base.gameObject.SetActive(false);
	}

	public IEnumerator SetPreviewScroll(List<GotchaReward> rewardList)
	{
		settingPreviewScroll = true;
		ReInitScroll();
		SetRewardList(rewardList);
		yield return null;
		wrapContent = content.gameObject.AddComponent<UIWrapContent>();
		wrapContent.itemSize = rewardUIwidth;
		wrapContent.cullContent = false;
		yield return null;
		scrollView.enabled = true;
		settingPreviewScroll = false;
	}

	public IEnumerator SpinEnumerator(GotchaReward resultReward)
	{
		while (settingPreviewScroll)
		{
			yield return null;
		}
		scrollView.enabled = false;
		float spinProgress2 = (float)(iterationsCount - iterationsCount % rewardButtonList.Count) * (float)rewardUIwidth;
		GotchaRewardUI resultButton = null;
		switch (resultReward.type)
		{
		case GotchaRewardType.Product:
		{
			GotchaReward resultReward2 = default(GotchaReward);
			resultButton = rewardButtonList.Find((GotchaRewardUI r) => r.reward == resultReward2);
			break;
		}
		case GotchaRewardType.Cash:
			resultButton = rewardButtonList.Find((GotchaRewardUI r) => r.reward.type == GotchaRewardType.Cash);
			break;
		case GotchaRewardType.Gold:
			resultButton = rewardButtonList.Find((GotchaRewardUI r) => r.reward.type == GotchaRewardType.Gold);
			break;
		}
		if (resultButton == null)
		{
			Debug.LogError("Can't find result!!!");
			yield break;
		}
		float resultButtonX = resultButton.transform.position.x / GotchaManagerUI.instance.uiRoot.transform.localScale.x;
		spinProgress2 = ((!(resultButtonX > 0f)) ? (spinProgress2 + ((float)rewardButtonList.Count * (float)rewardUIwidth - resultButtonX)) : (spinProgress2 - resultButtonX));
		spinProgress2 += Random.Range((float)(-rewardUIwidth) * 0.3f, (float)rewardUIwidth * 0.3f);
		newScrollPosition = new Vector3(spinProgress2, 0f, 0f);
		newScrollPosition += scrollView.transform.localPosition;
		newScrollPosition.y = scrollView.transform.localPosition.y;
		yield return null;
		StartFlashbulbsSpinAnimations();
	}

	private void Update()
	{
		if (newScrollPosition != Vector3.zero)
		{
			if (!scrollView.transform.localPosition.AlmostEquals(newScrollPosition, 50f))
			{
				anumarionTime = Mathf.Lerp(anumarionTime, 0.75f, Time.deltaTime * 0.06f);
				scrollView.transform.localPosition = Vector3.Lerp(scrollView.transform.localPosition, newScrollPosition, Time.deltaTime * lerpMiltiplayer);
				scrollViewPanel.clipOffset = new Vector3(0f - scrollView.transform.localPosition.x, 0f);
				lerpMiltiplayer = Mathf.Lerp(lerpMiltiplayer, 1f, Time.deltaTime * 0.75f);
			}
			else
			{
				lerpMiltiplayer = 0f;
				CoroutineRunner.Instance.StartCoroutine(ShowRewardWithDelay());
				StartFlashbulbsBlinkAnimations();
				newScrollPosition = Vector3.zero;
			}
		}
	}

	private IEnumerator ShowRewardWithDelay()
	{
		yield return new WaitForSeconds(1f);
		GotchaManagerUI.instance.ShowRewardPanel();
	}

	private void OnDisable()
	{
		newScrollPosition = Vector3.zero;
	}

	private void ReInitScroll()
	{
		newScrollPosition = Vector3.zero;
		base.gameObject.SetActive(true);
		ClearRewards();
		if ((bool)wrapContent)
		{
			wrapContent.mScroll = null;
			Object.Destroy(wrapContent);
			wrapContent = null;
		}
		scrollViewPanel.transform.localPosition = new Vector3(0f, scrollViewPanel.transform.localPosition.y, 0f);
		scrollViewPanel.clipOffset = Vector2.zero;
		content.localPosition = startContentPosition;
	}

	private void SetRewardList(List<GotchaReward> rewardList)
	{
		bool flag = false;
		bool flag2 = false;
		int num = 0;
		bool flag3 = false;
		foreach (GotchaReward reward in rewardList)
		{
			switch (reward.type)
			{
			case GotchaRewardType.Product:
				SetRewardButton(reward, num, flag3);
				num++;
				break;
			case GotchaRewardType.Cash:
				if (!flag2)
				{
					SetRewardButton(reward, num, flag3);
					flag2 = true;
					num++;
				}
				break;
			case GotchaRewardType.Gold:
				if (!flag)
				{
					SetRewardButton(reward, num, flag3);
					flag = true;
					num++;
				}
				break;
			}
			flag3 = !flag3;
		}
	}

	private void SetRewardButton(GotchaReward reward, int index, bool color)
	{
		GotchaRewardUI gotchaRewardUI = Object.Instantiate(rewardButton);
		gotchaRewardUI.transform.SetParent(content);
		gotchaRewardUI.transform.localScale = Vector3.one;
		gotchaRewardUI.transform.localPosition = new Vector3(rewardUIwidth * index, 0f, 0f);
		gotchaRewardUI.gameObject.SetActive(true);
		gotchaRewardUI.Set(reward, false, color);
		rewardButtonList.Add(gotchaRewardUI);
	}

	public void ClearRewards()
	{
		for (int i = 0; i < rewardButtonList.Count; i++)
		{
			Object.Destroy(rewardButtonList[i].gameObject);
		}
		rewardButtonList.Clear();
	}
}
